﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Event.ReactiveProgramming
{
    public partial class ReactiveForm : Form
    {
        public ReactiveForm()
        {
            InitializeComponent();

            BindFirstGroupButtons();
            BindSecondGroupButtons();
            BindThirdGroupButtons();
        }

        //********************事件模型和反应式模型***********************
        private void BindFirstGroupButtons()
        {
            btnFirstEventMode.Click += btnFirstEventMode_Click;

            //var clickedStream = Observable.FromEventPattern<EventArgs>(btnFirstReactiveMode,"Click");
            //clickedStream.Subscribe(e => MessageBox.Show("Hello world"));

            btnFirstReactiveMode.FromClickEventPattern()
                .Subscribe(e => MessageBox.Show("hello world"));

        }

        void btnFirstEventMode_Click(object sender, EventArgs e)
        {
            MessageBox.Show("hello world");
        }
        //********************事件模型和反应式模型***********************


        //********************异步事件模型和反应式模型***********************
        private void BindSecondGroupButtons()
        {
            btnSecondEventMode.Click += btnSecondEventMode_Click;

            btnSecondReactiveMode.FromClickEventPattern()
                .ObserveOn(new NewThreadScheduler())
                .Subscribe(e =>
                {
                    btnSecondReactiveMode.BackColor = Color.Coral;
                    Thread.Sleep(5000);
                    MessageBox.Show("hello world");
                });

            BtnSecondEventAsyncModel.Click += BtnSecondEventAsyncModel_Click;

            btnSecondAsyncAwaitMode.Click += BtnSecondAsyncAwaitModeClick;
        }

        void BtnSecondEventAsyncModel_Click(object sender, EventArgs e)
        {
            Action action = () =>
            {
                Task.Run(() =>
                {
                    BtnSecondEventAsyncModel.BackColor = Color.Coral;
                    Thread.Sleep(5000);
                    MessageBox.Show("hello world");
                });

            };
            BeginInvoke(action);
        }

        void btnSecondEventMode_Click(object sender, EventArgs e)
        {
            btnSecondEventMode.BackColor = Color.Coral;
            Thread.Sleep(5000);
            MessageBox.Show("hello world");
        }

        async void BtnSecondAsyncAwaitModeClick(object sender, EventArgs e)
        {
            await LongTimeOperator();
        }

        async Task LongTimeOperator()
        {
            btnSecondAsyncAwaitMode.BackColor = Color.Coral;
            await Task.Delay(5000).ContinueWith(t => MessageBox.Show("hello world"));
        }
        //********************异步事件模型和反应式模型***********************


        //********************事件流***************************************
        private void BindThirdGroupButtons()
        {
            var increasedEventStream = BtnIncreasement.FromClickEventPattern()
                .Select(_ => 1);
            var decreasedEventStream = btnDecrement.FromClickEventPattern()
                .Select(_ => -1);

            increasedEventStream.Merge(decreasedEventStream)
                .Scan(0, (result, s) => result + s)
                .Subscribe(x => lblResult.Text = x.ToString());
        }
        //********************事件流***************************************

       
    }
}
